package com.spdzen.service.impl;

import com.spdzen.model.SysResource;
import com.spdzen.model.SysRoleResource;
import com.spdzen.mapper.SysRoleResourceMapper;
import com.spdzen.service.SysResourceService;
import com.spdzen.service.SysRoleResourceService;
import com.spdzen.support.SimpleServiceImpl;
import com.spdzen.support.SimpleTreeNode;
import com.spdzen.support.shiro.ShiroDynamicAuthorization;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 角色功能关联  服务实现类
 */
@Service("sysRoleResourceService")
public class SysRoleResourceServiceImpl extends SimpleServiceImpl<SysRoleResourceMapper, SysRoleResource> implements SysRoleResourceService {
	@Resource
	private SysResourceService sysResourceService;
	
	@Override
	public List<SimpleTreeNode<SysResource>> selectTree(Long sysRoleId) {
		SysRoleResource record = new SysRoleResource();
		record.setSysRoleId(sysRoleId);
		List<SysRoleResource> roleResources = selectList(record);
		Map<Long, SysRoleResource> map = new HashMap<Long, SysRoleResource>();
		for(SysRoleResource item : roleResources) {
			map.put(item.getSysResourceId(), item);
		}
		
		List<SysResource> resources = sysResourceService.selectAll();
		List<SimpleTreeNode<SysResource>> nodes = new ArrayList<SimpleTreeNode<SysResource>>();
		
		for(SysResource item : resources) {
			SimpleTreeNode<SysResource> node = new SimpleTreeNode<>();
			node.setId(item.getId().toString());
			
			Long parentId = item.getParentId();
			if(parentId == null){
				node.setParentId("0");
			}else{
				node.setParentId(parentId.toString());
			}
			
			node.setName(item.getName());
			node.setNodeData(item);
			node.setChecked(map.containsKey(item.getId()));
			node.setOpen(true);
			
			nodes.add(node);
		}
		
		return SimpleTreeNode.buildTree(nodes);
	}
	
	@Override
	@Transactional(rollbackFor = {Exception.class})
	@ShiroDynamicAuthorization
	public void bindResources(Long sysRoleId, List<Long> sysResourceIds) throws Exception {
		deleteByRole(sysRoleId);
		
		if(sysResourceIds == null || sysResourceIds.size() == 0) {
			return;
		}
		
		for(Long item : sysResourceIds) {
			SysRoleResource record = new SysRoleResource();
			record.setSysResourceId(item);
			record.setSysRoleId(sysRoleId);
			
			baseMapper.insert(record);
		}
	}
	
	@Override
	public int deleteByRole(Long sysRoleId) {
		SysRoleResource record = new SysRoleResource();
		record.setSysRoleId(sysRoleId);
		return delete(record);
	}
	
	@Override
	public int deleteByResource(Long sysResourceId) {
		SysRoleResource record = new SysRoleResource();
		record.setSysResourceId(sysResourceId);
		return delete(record);
	}
	
	@Override
	public List<SysRoleResource> selectByRole(Long sysRoleId) {
		SysRoleResource record = new SysRoleResource();
		record.setSysRoleId(sysRoleId);
		return selectList(record);
	}
}
